package org.jod.anarchytrek.timer;

import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;

import org.jod.anarchytrek.ITimer;
import org.jod.anarchytrek.ITimerableComponent;


/*
 * 
 * ( data.getYear() - 2323 + ( ( numgiorno(data) + ( (data.getHours() + (data.getMinutes()/60)) /24 ) ) / 365) )*1000
 */

public class JodTimer implements ITimer {

    private static final int INIZIO_DATA_STELLARE = 1323;
    private GregorianCalendar _calendar;
    private List _timerables;
    

    public JodTimer() {
        this(new Date());
    }
    
    public JodTimer(Date aStartDate) {
        _calendar = new GregorianCalendar();
        _calendar.setTime(aStartDate);
        _timerables = new ArrayList();
    }
    
    public double dataStellare() {
        return ( _calendar.get(GregorianCalendar.YEAR)  - INIZIO_DATA_STELLARE + (double)( ( (double)_calendar.get(GregorianCalendar.DAY_OF_MONTH)+ ( ((double)_calendar.get(GregorianCalendar.HOUR_OF_DAY) + ((double)_calendar.get(GregorianCalendar.MINUTE)/60)) /24 ) ) / 365) )*1000;
    }
    
    public void register(ITimerableComponent aComponent) {
        _timerables.add(aComponent);
    }    

    public void increases() {
        add();
        changed();
    }

    private void add() {
        _calendar.add(GregorianCalendar.SECOND, 1);
    }
    
    private void changed() {
        Iterator iterTimerable = _timerables.listIterator();
        while (iterTimerable.hasNext()) {
            ( (ITimerableComponent) iterTimerable.next()).timed();
        }
    }

    public void start() {}

    public void stop() {}
    
    public void interval(long aInterval) {}




}
